package titt.util.deploy;

import java.io.ByteArrayOutputStream;
import java.util.zip.GZIPOutputStream;

public class GZipCache implements Cache {

	private final Cache c;
	private byte[] b;
	
	public GZipCache(Cache c) {
		this.c = c;
	}
	
	@Override
	public String getMimeType() {
		return c.getMimeType();
	}

	@Override
	public byte[] getValue() {
		return b;
	}

	@Override
	public boolean isOutdated() {
		return c.isOutdated();
	}

	@Override
	public synchronized void update() throws Exception {
		c.update();
		
		ByteArrayOutputStream bo = new ByteArrayOutputStream();
		GZIPOutputStream o = new GZIPOutputStream(bo);
		
		byte[] cb = c.getValue();

		o.write(cb, 0, cb.length);
		o.finish();
		o.flush();
		
		b = bo.toByteArray();
	}

}
